package com.sdksoft.readcard

import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.MenuItem
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.csht.readcard.ReadCardOutMethod
import com.csht.readcard.ReadSettingBuilder
import com.sdksoft.readcard.databinding.ActivityBaseBinding
import com.sdksoft.readcard.utils.ReadCardManagerUtil
import com.sdksoft.readcard.utils.WaterMarkBg
import com.csht.utils.DevicesUtils

open class BaseInitActivity : AppCompatActivity() {

    private val Permission = arrayOf(
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.INTERNET,
        Manifest.permission.ACCESS_NETWORK_STATE,
        Manifest.permission.NFC,
        Manifest.permission.CAMERA,
        Manifest.permission.READ_PHONE_STATE
    )

    protected lateinit var baseActivityBinding: ActivityBaseBinding
    // 读卡SDK
    protected lateinit var readSettingBuilder: ReadSettingBuilder
    protected lateinit var readCardManager: ReadCardOutMethod
    protected val mainHandler = Handler(Looper.getMainLooper())
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        baseActivityBinding = ActivityBaseBinding.inflate(layoutInflater)
        setContentView(baseActivityBinding.root)
        setMarkText(baseActivityBinding.tvMark)
        initToolBar()
        val list: MutableList<String> = mutableListOf()
        list.apply {
            add(Manifest.permission.BLUETOOTH)
            add(Manifest.permission.BLUETOOTH_ADMIN)
            add(Manifest.permission.ACCESS_FINE_LOCATION)
            add(Manifest.permission.ACCESS_COARSE_LOCATION)
        }
        list.addAll(Permission)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            list.add(Manifest.permission.BLUETOOTH_SCAN)
            list.add(Manifest.permission.BLUETOOTH_ADVERTISE)
            list.add(Manifest.permission.BLUETOOTH_CONNECT)
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(list.toTypedArray(), 100)
        }
        initBuilder()
        initView()
    }

    private fun initToolBar() {
        baseActivityBinding.apply {
            setSupportActionBar(toolbar)
            toolbar.title = ""
            tvTitle.text = getString(R.string.app_name)
            supportActionBar?.let {
                it.setDisplayShowTitleEnabled(false)
                it.setHomeAsUpIndicator(R.drawable.ic_arrow_white_24dp)
                it.setDisplayHomeAsUpEnabled(false)
            }
        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            android.R.id.home -> finish()
        }
        return super.onOptionsItemSelected(item)
    }

    open fun initView() {
        baseActivityBinding.apply {
            tvVersion.text = "v${BuildConfig.VERSION_NAME}\n${DevicesUtils.getDeviceId(baseContext)}"
            tvVersion.setTextIsSelectable(true)
        }
    }

    fun setTitleText(title: String, isShowBackView: Boolean = true) {
        baseActivityBinding.apply {
            supportActionBar?.let {
                it.setDisplayHomeAsUpEnabled(isShowBackView)
            }
            toolbar.title = ""
            tvTitle.text = title
        }
    }

    /**
     * 统一配置读卡SDK参数信息
     * 测试使用的 appid 不可以用于商用(否则后果自负)。
     * 商用的 appid 请务必联系我司商务进行分配。
     */
    open fun initBuilder() {
        readSettingBuilder = ReadCardManagerUtil.init.getReadSettingBuilder()
        readCardManager = readSettingBuilder.buildCreate()
    }

    /**
     * 设置水印
     */
    open fun setMarkText(markText :View){
        val labels: MutableList<String> = ArrayList()
        labels.add("读卡demo")
        markText.setBackgroundDrawable(WaterMarkBg(this, labels, -30, 15))
    }

    fun hasNFC(context: Context): Boolean {
        val packageManager = context.packageManager
        return packageManager.hasSystemFeature(PackageManager.FEATURE_NFC)
    }
}